using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using PHSRAG.Insight21.BusinessObjects;
using DataAccess = PHSRAG.Utility.DataAccess;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;

namespace PHSRAG.Insight21.Admin
{
	public class ResearchContact : SmartBusinessObject
	{
		#region Enum Database Columns
		/// <summary>
		/// Columns of ResearchFinanceInfo Table
		/// </summary>
		public enum ResearchFinanceInfoColumns
		{
			Id,
			FwkDomainOrganizationId,
			AdmSponsorTypeId,
			AdmPersonId,
			DateLastModified,
			DateCreated
		}
		#endregion

		#region Constants
		private const string ResearchFinanceInfoKey = "ResearchFinanceInfo_EE55E7B3-DA44-4fc3-869A-C2A568E1E4CE";
		private const string ResearchFinanceInfoTableName = "ResearchFinanceInfo";
		public const string ColumnId = "Id"; //Part of SBO
		public const string ColumnFwkDomainOrganizationId = "FwkDomainOrganizationId";
		public const string ColumnInstitutionId = "InstitutionId";
		public const string ColumnInstitutionName = "Institution";
		public const string ColumnChiefCode = "ChiefCode";
		public const string ColumnAdmSponsorTypeId = "AdmSponsorTypeId";
		public const string ColumnAdmSponsorType = "AdmSponsorTypeName";
		public const string ColumnAdmPersonId = "AdmPersonId";
		public const string ColumnUserName = "UserName";
		public const string ColumnFullName = "FullName";
		public const string ColumnDateLastModified = "DateLastModified";
		public const string ColumnDateCreated = "DateCreated";
      
        public const string ColumnRoleId = "RoleId";
        public const string ColumnRole = "Role";
		#endregion

		#region Instance Variables
		private int fwkDomainOrganizationId;
		private int institutionId;
		private string chiefCode;
		private int admSponsorTypeId;
		private int admPersonId;
       
        private int roleId;
        private string role;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new ResearchFinanceInfo object based on the Cache Policy passed in as a parameter. 
		/// All the instance variables will be set to their default values.
		/// </summary>
		/// <param name="cachePolicy">Cache Policy which specifies where the ResearchFinanceInfo object should be cached</param>
		public ResearchContact(ICachePolicy cachePolicy)
			: base(cachePolicy)
		{
		}

		public ResearchContact(ICachePolicy cachePolicy, DataRow dataRow)
			: base(cachePolicy)
		{
			PopulateMembers(dataRow);
		}
		#endregion

		#region Properties

		/// <summary>
		/// Gets or sets FwkDomainOrganizationId
		/// </summary>
		public int FwkDomainOrganizationId
		{
			get { return fwkDomainOrganizationId; }
			set { SetDirty(ref fwkDomainOrganizationId, value); }
		}

		/// <summary>
		/// Gets or sets InstitutionId
		/// </summary>
		public int InstitutionId
		{
			get { return institutionId; }
			set
			{
				if (value != institutionId)
				{
					institutionId = value;
				}
			}
		}
      
        public int RoleId
        {
            get { return roleId; }
            set
            {
                if (value != roleId)
                {
                    roleId = value;
                }
            }
        }
		/// <summary>
		/// Gets or sets ChiefCode
		/// </summary>
		public string ChiefCode
		{
			get { return chiefCode; }
			set
			{
				if (value != chiefCode)
				{
					chiefCode = value;
				}
			}
		}

      
        public string Role
        {
            get { return role; }
            set
            {
                if (value != role)
                {
                    role = value;
                }
            }
        }
		/// <summary>
		/// Gets or sets AdmSponsorTypeId
		/// </summary>
		public int AdmSponsorTypeId
		{
			get { return admSponsorTypeId; }
			set { SetDirty(ref admSponsorTypeId, value); }
		}

		/// <summary>
		/// Gets or sets AdmPersonId
		/// </summary>
		public int AdmPersonId
		{
			get { return admPersonId; }
			set { SetDirty(ref admPersonId, value); }
		}
		#endregion

		#region SmartBusinessObject methods
		/// <summary>
		/// Determines if the object is dirty based on whether or not atleast one of its properties have changed
		/// </summary>
		/// <returns>true - if the object is dirty, false - if not.</returns>
		public override bool IsDirty()
		{
			return dirty;
		}


		/// <summary>
		/// Loads the ResearchFinanceInfo object from the database with the specified id. 
		/// </summary>
		/// <param name="connectionString">Information to connect to the database</param>
		/// This method does not fetch the actual contents(blob) of the file.
		/// <param name="args">Id of the ResearchFinanceInfo object to be loaded</param>
		public override void Load(string connectionString, params object[] args)
		{
			throw new Exception("The method or operation is not implemented.");
		}


		/// <summary>
		/// Saves the instance of the ResearchFinanceInfo object to the database. 
		/// The stored procedure inserts a new record if the id of the object is 0, 
		/// otherwise the procedure updates the record with the matching ResearchFinanceInfo id
		/// </summary>
		/// <param name="sqlTransaction">The context in which the insert/update should be executed</param>
		protected override void SaveObject(System.Data.SqlClient.SqlTransaction sqlTransaction)
		{
			try
			{
				fwkDomainOrganizationId = Convert.ToInt32(DataAccess.ExecuteScalar(sqlTransaction.Connection, sqlTransaction,
					"GetFwkDomainOrganizationId",
				DataAccess.BuildSqlParameterArray(
				"@institutionId", SqlDbType.Int, institutionId,
				"@chiefCode", SqlDbType.VarChar, chiefCode)));
				// the stored procedure will determine whether to insert or update an existing ResearchFinanceInfo object.
				Id = Convert.ToInt32(DataAccess.ExecuteScalar(sqlTransaction.Connection, sqlTransaction, "SaveResearchFinanceInfo",
					DataAccess.BuildSqlParameterArray(
					"@id", SqlDbType.Int, Id,
					"@fwkDomainOrganizationId", SqlDbType.Int, fwkDomainOrganizationId,
					"@admSponsorTypeId", SqlDbType.Int, admSponsorTypeId,
					"@admPersonId", SqlDbType.Int, admPersonId,
                    "@roleId",SqlDbType.Int,roleId)));
			}
			catch (Exception e)
			{
				Utility.ExceptionReport.WriteToEventLog("Failed to save the ResearchFinanceInfo object", e, true, Utility.ExceptionReport.ReportType.Error);
				throw;
			}
		}


		/// <summary>
		/// Executes the DeleteResearchFinanceInfo stored procedure passing in the id as the parameter
		/// </summary>
		/// <param name="sqlTransaction">The transaction context in which the SQL statement is to be executed</param>
		protected override void DeleteObject(System.Data.SqlClient.SqlTransaction sqlTransaction)
		{
			try
			{
				Utility.DataAccess.ExecuteNonQuery(sqlTransaction.Connection, sqlTransaction, "DeleteResearchFinanceInfo",
					Utility.DataAccess.BuildSqlParameterArray("@id", SqlDbType.Int, Id));
			}
			catch (Exception e)
			{
				Utility.ExceptionReport.WriteToEventLog(string.Format("Failed to delete the ResearchFinanceInfo object with id: {0}", Id), e, true, Utility.ExceptionReport.ReportType.Error);
				throw;
			}
		}


		/// <summary>
		/// Cache this instance using the prevalent cache policy.
		/// </summary>
		public override void Cache()
		{
			CachePolicy.Cache(CacheKey(), this);
		}


		/// <summary>
		/// Remove this instance from the cache using the prevalent cache policy.
		/// </summary>
		public override void UnCache()
		{
			CachePolicy.UnCache(CacheKey());
		}

		/// <summary>
		/// Cache Key uniquely identifies this object within the cache.
		/// </summary>
		/// <returns>the unique string for this object.</returns>
		public override string CacheKey()
		{
			return ResearchFinanceInfoKey;
		}
		#endregion

		#region Private methods
		/// <summary>
		/// Populates instance fields with corresponding values from the data row
		/// </summary>
		/// <param name="dataRow">A DataRow object holding the values to be populated to the instance fields</param>
		private void PopulateMembers(DataRow dataRow)
		{
			Id = (int)dataRow[ColumnId];
			fwkDomainOrganizationId = (int)dataRow[ColumnFwkDomainOrganizationId];
			institutionId = (int)dataRow[ColumnInstitutionId];
			chiefCode = (string)dataRow[ColumnChiefCode];
			admSponsorTypeId = (int)dataRow[ColumnAdmSponsorTypeId];
			admPersonId = (int)dataRow[ColumnAdmPersonId];
            roleId = (int)dataRow[ColumnRoleId];
            role = (string)dataRow[ColumnRole];
		}
		#endregion
	}
}
